package com.edu.study.base.needfieldMetadata.controller;

import com.alibaba.fastjson.JSONObject;
import com.edu.study.base.needfieldMetadata.domain.ColumnData;
import com.edu.study.base.needfieldMetadata.domain.NeedfieldMetadata;
import com.edu.study.base.needfieldMetadata.domain.TbNeedfieldMetadataRy;
import com.edu.study.base.needfieldMetadata.service.ITbNeedfieldMetadataRyService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.ColumnUtil;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.uuid.IdUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 显示项配置Controller
 *
 * @author jjt
 * @date 2021-03-11
 */
@RestController
@RequestMapping("/metadataconfig/needfieldMetadata")
public class TbNeedfieldMetadataRyController extends BaseController
{
    @Autowired
    private ITbNeedfieldMetadataRyService tbNeedfieldMetadataRyService;

    /**
     * 查询显示项配置列表
     */
    @PreAuthorize("@ss.hasPermi('metadataconfig:needfieldMetadata:list')")
    @GetMapping("/list")
    public TableDataInfo list(TbNeedfieldMetadataRy tbNeedfieldMetadataRy)
    {
        startPage();
        List<TbNeedfieldMetadataRy> list = tbNeedfieldMetadataRyService.selectTbNeedfieldMetadataRyList(tbNeedfieldMetadataRy);
        return getDataTable(list);
    }

    /**
     * 查询配置信息
     * @param tbNeedfieldMetadataRy
     * @return
     */
    @GetMapping("/getNeedFieldMetaData")
    public AjaxResult getNeedFieldMetaData(TbNeedfieldMetadataRy tbNeedfieldMetadataRy)
    {
        List<TbNeedfieldMetadataRy> list = tbNeedfieldMetadataRyService.selectTbNeedfieldMetadataRyList(tbNeedfieldMetadataRy);
        if(list!=null && list.size()>0){
            return AjaxResult.success(list.get(0));
        }
        return AjaxResult.success();
    }




    /**
     * 导出显示项配置列表
     */
    @PreAuthorize("@ss.hasPermi('metadataconfig:needfieldMetadata:export')")
    @Log(title = "显示项配置", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TbNeedfieldMetadataRy tbNeedfieldMetadataRy)
    {
        List<TbNeedfieldMetadataRy> list = tbNeedfieldMetadataRyService.selectTbNeedfieldMetadataRyList(tbNeedfieldMetadataRy);
        ExcelUtil<TbNeedfieldMetadataRy> util = new ExcelUtil<TbNeedfieldMetadataRy>(TbNeedfieldMetadataRy.class);
        return util.exportExcel(list, "needfieldMetadata");
    }

    /**
     * 获取显示项配置详细信息
     */
    @PreAuthorize("@ss.hasPermi('metadataconfig:needfieldMetadata:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return AjaxResult.success(tbNeedfieldMetadataRyService.selectTbNeedfieldMetadataRyById(id));
    }


    /**
     * 新增显示项配置
     */
    @PreAuthorize("@ss.hasPermi('metadataconfig:needfieldMetadata:add')")
    @Log(title = "显示项配置", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TbNeedfieldMetadataRy tbNeedfieldMetadataRy)
    {
        tbNeedfieldMetadataRy.setId(IdUtils.simpleUUID());
        tbNeedfieldMetadataRy.setCreateTime(new Date());
        tbNeedfieldMetadataRy.setCreateBy(SecurityUtils.getUserId().toString());
        return toAjax(tbNeedfieldMetadataRyService.insertTbNeedfieldMetadataRy(tbNeedfieldMetadataRy));
    }

    /**
     * 显示字段配置
     * @param needfieldMetadata
     * @return
     */
    @Log(title = "保存表配置信息", businessType = BusinessType.INSERT)
    @PostMapping("saveNeedfieldMetadata")
    public AjaxResult saveNeedfieldMetadata(@RequestBody NeedfieldMetadata needfieldMetadata){
        if(needfieldMetadata!=null){
            List<ColumnData> columnDataList = needfieldMetadata.getColumnDataList();
            if(null != columnDataList && columnDataList.size() > 0){
                String showColumns = "";
                String mustInputFields = "";
                Map showFieldsTitle = new HashMap<>();
                Map showFieldsTip = new HashMap<>();
                for (ColumnData columnData : columnDataList) {
                    String columnName = ColumnUtil.columnToUpperCase(columnData.getColumnName());
                    showColumns += columnName + ",";
                    if(columnData.isMustInputField()){
                        mustInputFields += columnName+",";
                    }
                    if(StringUtils.isNotEmpty(columnData.getFieldTitle())){
                        showFieldsTitle.put(columnName,columnData.getFieldTitle());
                    }
                    if(StringUtils.isNotEmpty(columnData.getFieldTip())){
                        showFieldsTip.put(columnName,columnData.getFieldTip());
                    }
                }
                TbNeedfieldMetadataRy needfieldMetadataRy = new TbNeedfieldMetadataRy();
                needfieldMetadataRy.setId(needfieldMetadata.getId());
                needfieldMetadataRy.setShowFields(showColumns.substring(0,showColumns.lastIndexOf(",")));
                if(mustInputFields.indexOf(",") > 0){
                    needfieldMetadataRy.setMustInputFields(mustInputFields.substring(0,mustInputFields.length()-1));
                }
                needfieldMetadataRy.setShowFieldsTitleJson(JSONObject.toJSONString(showFieldsTitle));
                needfieldMetadataRy.setShowFieldsTipJson(JSONObject.toJSONString(showFieldsTip));
                int i = tbNeedfieldMetadataRyService.updateTbNeedfieldMetadata(needfieldMetadataRy);
            }
        }

        return AjaxResult.success();

    }

    /**
     * 修改显示项配置
     */
    @PreAuthorize("@ss.hasPermi('metadataconfig:needfieldMetadata:edit')")
    @Log(title = "显示项配置", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TbNeedfieldMetadataRy tbNeedfieldMetadataRy)
    {
        tbNeedfieldMetadataRy.setUpdateTime(new Date());
        tbNeedfieldMetadataRy.setUpdateBy(SecurityUtils.getUserId().toString());
        return toAjax(tbNeedfieldMetadataRyService.updateTbNeedfieldMetadataRy(tbNeedfieldMetadataRy));
    }

    /**
     * 删除显示项配置
     */
    @PreAuthorize("@ss.hasPermi('metadataconfig:needfieldMetadata:remove')")
    @Log(title = "显示项配置", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(tbNeedfieldMetadataRyService.deleteTbNeedfieldMetadataRyByIds(ids));
    }
}
